package com.hkfn.rfb.common.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.bean.BannerModel;
import com.hkfn.rfb.common.utils.ScreenUtils;

public class BarFloorAdapter extends BaseRecyclerAdapter<BannerModel> {

    private Context context;

    private int currrentPosition = 0;

    public int getCurrrentPosition() {
        return currrentPosition;
    }

    public void setCurrrentPosition(int currrentPosition) {
        this.currrentPosition = currrentPosition;
        notifyDataSetChanged();
    }

    public BarFloorAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bar_floor, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, BannerModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_title;
        private View v_line;
        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.v_line = itemView.findViewById(R.id.v_line);

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        if (getItemCount() > 6){
            layoutParams.width = ScreenUtils.getScreenWidth(context) / 6;
        }else {
            layoutParams.width = ScreenUtils.getScreenWidth(context) / getItemCount();
        }
    }

    private void setItemData(MyViewHolder viewHolder, BannerModel data, int position) {
        viewHolder.tv_title.setText(data.getTitle());
        if (position == currrentPosition){
            viewHolder.tv_title.setTextColor(context.getResources().getColor(R.color.main_color));
            viewHolder.v_line.setVisibility(View.VISIBLE);
        }else {
            viewHolder.tv_title.setTextColor(context.getResources().getColor(R.color.text_color));
            viewHolder.v_line.setVisibility(View.INVISIBLE);
        }
    }
}
